#include <bits/stdc++.h>
using namespace std;
int main() {
  // find key -> words      -> unordered_map
  // words  order unique    ->  set
  unordered_map<string, set<int>> mp;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i ++) {
    int m;
    cin >> m;
    while (m--) {
      string str;
      cin >> str;
      mp[str].insert(i);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    string str;
    cin >> str;
    for (auto &x : mp[str]) {
      cout << x << ' ';
    }
    cout << endl;
  }
}